package com.soccertgc.core
{
	import com.soccertgc.logging.Dumper;
	import com.soccertgc.logging.IDumpable;

	public class Player extends DataObject implements IDumpable
	{
		
		// -------------------------------------------------------------------------------
		// CONSTANTS
		// -------------------------------------------------------------------------------	
		
		// JSON Property names
		protected static const 	JSON_PROPERTY_USERNAME:String			= "username";
		protected static const 	JSON_PROPERTY_PASSWORD:String			= "password";
		protected static const 	JSON_PROPERTY_FIRST_NAME:String			= "firstName";
		protected static const 	JSON_PROPERTY_LAST_NAME:String			= "lastName";
		protected static const 	JSON_PROPERTY_REG_DATE:String			= "regDate";
		protected static const 	JSON_PROPERTY_EMAIL:String				= "email";
		
		// -------------------------------------------------------------------------------
		// FIELDS
		// -------------------------------------------------------------------------------
		
		protected var _firstName:String;
		protected var _lastName:String;
		protected var _username:String;
		protected var _email:String;
		protected var _registration:Date;
		
		// -------------------------------------------------------------------------------
		// CONSTRUCTOR
		// -------------------------------------------------------------------------------
		
		public function Player(data:Object = null)
		{
			// Clear
			clear();
			// If data is provided, initializes the instance
			if (data != null) load(data);
		}
		
		// -------------------------------------------------------------------------------
		// PROPERTIES
		// -------------------------------------------------------------------------------
		
		[Bindable]
		public function get username():String
		{
			return _username;
		}
		
		public function set username(value:String):void
		{
			_username = value;
		}
		
		[Bindable]
		public function get firstName():String
		{
			return _firstName;
		}
		
		public function set firstName(value:String):void
		{
			_firstName = value;
		}
		
		[Bindable]
		public function get lastName():String
		{
			return _lastName;
		}
		
		public function set lastName(value:String):void
		{
			_lastName = value;
		}
		
		[Bindable]
		public function get email():String
		{
			return _email;
		}
		
		public function set email(value:String):void
		{
			_email = value;
		}
		
		[Bindable]
		public function get registration():Date
		{
			return _registration;
		}
		
		public function set registration(value:Date):void
		{
			_registration = value;
		}
		
		// -------------------------------------------------------------------------------
		// PRIVATE METHODS
		// -------------------------------------------------------------------------------
		
		// DataObject implementation
		
		override protected function validate(data:Object):void
		{
			// Required fields
			if (!data.hasOwnProperty(JSON_PROPERTY_USERNAME))	throwLoadMissingPropError(JSON_PROPERTY_USERNAME);
		}
		
		override protected function fill(data:Object):void
		{
			// Required fields
			_username 	= data[JSON_PROPERTY_USERNAME];
			_firstName	= data[JSON_PROPERTY_FIRST_NAME];
			_lastName	= data[JSON_PROPERTY_LAST_NAME];
			_email		= data[JSON_PROPERTY_EMAIL];
			// LT_TODO: parse date
			// _registration	= null;
		}
		
		// -------------------------------------------------------------------------------
		// PUBLIC METHODS
		// -------------------------------------------------------------------------------
		
		public function equals(player:Player):Boolean
		{
			return	_firstName		== player.firstName	&& 
					_lastName		== player.lastName	&&
					_username		== player.username	&&
					_email			== player.email		&&
					_registration	== player.registration;
		}
		
		public function copy(player:Player):void
		{
			_firstName		= player.firstName; 
			_lastName		= player.lastName;
			_username		= player.username;
			_email			= player.email;
			_registration	= player.registration;			
		}
		
		public function clone():Player
		{
			var player:Player = new Player();
			player.copy(this);
			return player;
		}
		
		override public function clear():void
		{
			_firstName		= "";
			_lastName		= "";
			_username		= "";
			_email			= "";
			_registration	= null;
		}
		
		public function dump(source:Array = null, indentDepth:int = 0):Array
		{
			return Dumper.dump(this, null, source, indentDepth);
		}
		
	}
}